﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;

namespace fanfu.Common
{
    /// <summary>
    /// 用于控件数据绑定的类
    /// </summary>
    public sealed class DataBindHelper
    {
        /// <summary>
        /// 向列表控件绑定数据源
        /// </summary>
        /// <param name="dataSource">需绑定的数据源对象，List，DataTable等等</param>
        /// <param name="control">控件对象</param>
        /// <param name="textField">显示文本字段名称</param>
        /// <param name="valueField">绑定值字段名称</param>
        /// <param name="addCustomerItem">如需添加自定义项，如“全部”，无自定义项则输入NULL</param>
        /// <param name="insertIndex">自定义项的插入位置</param>
        public static void BindData(object dataSource,
                                    ListControl control,
                                    string textField,
                                    string valueField,
                                    ListItem addCustomerItem,
                                    int insertIndex)
        {
            control.DataSource = dataSource;
            control.DataTextField = textField;
            control.DataValueField = valueField;
            control.DataBind();
            if (addCustomerItem != null)
            {
                control.Items.Insert(insertIndex, addCustomerItem);
            }
        }

        /// <summary>
        /// 向列表控件绑定数据源
        /// </summary>
        /// <param name="dataSource">需绑定的数据源对象，List，DataTable等等</param>
        /// <param name="control">控件对象</param>
        /// <param name="textField">显示文本字段名称</param>
        /// <param name="valueField">绑定值字段名称</param>
        /// <param name="addCustomerItem">向集合的首位置插入自定义项，如“全部”，无则输入NULL</param>
        public static void BindData(object dataSource,
                                    ListControl control,
                                    string textField,
                                    string valueField,
                                    ListItem addCustomerItem)
        {
            BindData(dataSource, control, textField, valueField, addCustomerItem, 0);
        }

        /// <summary>
        /// 向列表控件绑定数据源
        /// </summary>
        /// <param name="dataSource">需绑定的数据源对象，List，DataTable等等</param>
        /// <param name="control">控件对象</param>
        /// <param name="textField">显示文本字段名称</param>
        /// <param name="valueField">绑定值字段名称</param>
        public static void BindData(object dataSource,
                                    ListControl control,
                                    string textField,
                                    string valueField)
        {
            BindData(dataSource, control, textField, valueField, null, 0);
        }
    }
}
